﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;
using System.Text.RegularExpressions;

namespace COFE.Core.Utils
{
    public static class WorkUtils
    {
        //public static void RegisterWorkType(Type workType, Func<string, string, IWork> constructMethod)
        //{
        //    if (workType.GetInterface("IWork") == null)
        //        throw new ArgumentException("workType is not an IWork.");
        //    var waArray = workType.GetCustomAttributes(typeof(WorkAttribute), true);
        //    if (waArray.Length == 0)
        //        throw new ArgumentException("Filter is not decorated by FilterAttribute.");
        //    var wa = waArray[0] as WorkAttribute;
        //    FactoryRegister<IWork>.RegisterFactory(wa.WorkTypeName, wa.WorkTypeName + "=", constructMethod,
        //        (p) => p.ConstructParameters);
        //}

        public static string FormatWorkName(string libraryAlias, string workType,
              string parameter)
        {
            return String.Format("{0}::{1}({2})",
                libraryAlias, workType, parameter);
        }

        public static bool ParseWorkName(string workName, out string libraryAlias,
            out string workType, out string parameter)
        {
            var match = new Regex(RegexPatterns.ParseWorkNamePattern).Match(workName);

            libraryAlias = null; workType = null; parameter = null;
            if (!match.Success)
                return false;

            libraryAlias = match.Groups["lib"].Value;
            workType = match.Groups["type"].Value;
            parameter = match.Groups["param"].Value;
            return true;
        }
    }
}
